package com.globallogic.javatrainings.sergiiivanov.view;

import java.io.PrintWriter;

import com.globallogic.javatrainings.sergiiivanov.model.MyMap;
import com.globallogic.javatrainings.sergiiivanov.model.UniversityException;
import com.globallogic.javatrainings.sergiiivanov.resources.Formating;
import com.globallogic.javatrainings.sergiiivanov.resources.Messages;

/**
 * Superclass for user interface<br>
 * Inherited classes widely used in controllers based on BasicController
 * Implements basic functions for displaying information to user
 * <p/>
 * see class InputController - class for receiving feedback from user
 * 
 * @author Sergey
 * 
 */
public class GUI {
    private static boolean mute = false;
    private static GUI instance;
    private static PrintWriter myOutput;
    private static final String NEW_PAGE = "\n\n\n------------------------------------------------------";

    /**
     * Provides default visual output object. Used in classes that have no
     * specified GUI object.<br>
     * 
     * @return - default GUI;
     */
    public static GUI getGUI() {
        if (instance == null) {
            instance = new GUI();
        }
        return instance;
    }

    private PrintWriter getOutput() {
        if (myOutput == null) {
            myOutput = new PrintWriter(System.out, true);
        }
        return myOutput;
    }

    public void newPage() {
        show(NEW_PAGE);
    }

    public void notImplemented() {
        show("Sorry, this section is not implemented yet");
    }

    public void show(String title) {
        if(!mute){
            getOutput().println(title);
        }
    }

    public void showArrayAsList(String[] menuItems) {
        showArrayAsList(menuItems, 0);
    }

    public void showArrayAsList(String[] menuItems, int offset) {
        for (int i = 0; i < menuItems.length; i++) {
            show(Formating.LIST_LINE_SPACER + (i + offset)
                    + Formating.LIST_LINE_TAB + menuItems[i]);
        }
    }

    public void error(String title, String details) {
        show("__________________________________________________"
                + "\n Error: " + title.trim() + "\n Reason: "
                + (details == null ? "none" : details.trim())
                + "\n I am sorry about this");
    }

    public void showLineOfList(String menuItem, int offset) {
        show(Formating.LIST_LINE_SPACER + (offset) + Formating.LIST_LINE_TAB
                + menuItem);
    }

    @Deprecated
    public void showMap(MyMap<String> map) {
        for (String key : map.getKeys()) {
            try {
                show(key + Formating.TABLE_SPACER + map.get(key));
            } catch (UniversityException e) {
                show("Error: Unable to display " + key);
            }
        }
    }

    public void showTableOrdered(String[][] table) {
        showTableOrdered(table, 0);
    }

    public void showTableOrdered(String[][] table, int offset) {
        String line;
        for (int row = 0; row < table.length; row++) {
            line = Integer.toString(row + offset) + Formating.TABLE_SPACER;
            for (int col = 0; col < table[row].length; col++) {
                line += table[row][col] + Formating.TABLE_SPACER;
            }
            show(line);
        }
    }

    public void showValidationError(String message) {
        show(message);
    }

    public void showWarning(String warning) {
        show("!WARNING! > " + warning);
    }

    public void pagePersonDetails(String name, String mobile, String email) {
        show(Messages.PERSON_DETAILS_NAME + Formating.TABLE_SPACER + name);
        show(Messages.PERSON_DETAILS_PHONE + Formating.TABLE_SPACER + mobile);
        show(Messages.PERSON_DETAILS_EMAIL + Formating.TABLE_SPACER + email);
    }

    public void mute(boolean flag) {
        mute = flag;
    }
}
